<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealId">
            <MainBodySelect
              v-model="form.dealId"
              parameters="deal"
              :disabled="!!form.id"
              @change="changeHandle"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司名称')" prop="companyName">
            <el-input v-model="form.companyName" :placeholder="$t('公司名称')" disabled />
          </el-form-item>
        </el-col>
      </el-row>
      <div class="area-header">
        <i style="color: #ff4949;margin-right: 4px;">*</i>
        <span class="area-header-title">{{ $t('项目指标明细') }}</span>
      </div>
      <el-table :data="form.indicatorsDetails" border class="nowrapTable formTable">
        <el-table-column :label="$t('指标名称')" align="center" prop="indicatorName" show-overflow-tooltip>
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('指标名称') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'indicatorsDetails.' + scope.$index + '.indicatorName'" :rules="rules.indicatorName">
              <el-input v-model="scope.row.indicatorName" show-word-limit :maxlength="100" :placeholder="$t('指标名称')" />
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('指标方向')" align="center" prop="indicatorDirection">
          <template slot="header" slot-scope="{}">
            <i style="color: #ff4949;margin-right: 4px;">*</i>
            <span>{{ $t('指标方向') }}</span>
          </template>
          <template slot-scope="scope">
            <el-form-item :prop="'indicatorsDetails.' + scope.$index + '.indicatorDirection'" :rules="rules.indicatorDirection">
              <el-select v-model="scope.row.indicatorDirection" :placeholder="$t('指标方向')">
                <el-option
                  v-for="(item, index) in indicatorDirectionOptions"
                  :key="index"
                  :value="item.dictValue"
                  :label="$t(item.dictLabel)"
                />
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column :label="$t('备注')" align="center" prop="remark">
          <template slot-scope="scope">
            <el-input v-model="scope.row.remark" type="textarea" :placeholder="$t('备注')" show-word-limit :maxlength="2000" :autosize="{ minRows: 2, maxRows: 4}" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="center" width="80">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="delIndicators(scope.$index)">{{ $t('删除') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="tol">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="addIndicators">{{ $t("添加") }}</el-button>
      </div>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:operational" />
    </div>
  </div>
</template>

<script>
    import { saveDealIndicatorsSetting, getDealIndicatorsSetting, checkRepeatCompany } from '@/api/deal/operational/setting/dealIndicatorsSetting'
    import { findAll } from '@/api/deal/operational/setting/dealOperationalIndicatorsSetting'
    import { getDealBase } from '@/api/base/deal/dealBase'
    import MainBodySelect from '@/components/MainBodySelect/index'
    export default {
      components: {
        MainBodySelect
      },
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
              // 指标方向
              indicatorDirectionOptions: [],
                // 表单校验
                rules: {
                  dealId: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
                  indicatorName: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
                  indicatorDirection: { required: true, message: this.$t('必填信息'), trigger: 'blur' }
                }
            }
        },
        created() {
          this.getDicts('indicator_direction').then(response => {
            this.indicatorDirectionOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealIndicatorsSetting(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    })
                } else {
                    this.reset()
                    findAll().then(res => {
                      this.form.indicatorsDetails = res.data
                      this.loading = false
                    })
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: undefined,
                    companyId: undefined,
                    companyName: undefined,
                    delFlag: undefined,
                    createBy: undefined,
                    updateBy: undefined,
                    createTime: undefined,
                    updateTime: undefined,
                    indicatorsDetails: []
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                this.$parent.$parent.formOpen = false
                // 刷新list页面
                this.$parent.$parent.getList()
            },
            /** 提交按钮 */
            submitForm: function() {
                this.$refs['form'].validate(async valid => {
                    if (valid) {
                        if (!this.form.indicatorsDetails || this.form.indicatorsDetails.length === 0) {
                          this.$message.error(this.$t('至少有一条指标明细'))
                          return
                        }
                        this.loading = true
                        let flag = false
                        if (!this.form.id) {
                          await checkRepeatCompany(this.form.companyId).then(res => {
                            flag = res.data
                          })
                        }
                        if (flag) {
                          this.$message({
                            type: 'warning',
                            message: this.$t('当前公司已存在于项目指标设置中')
                          })
                          this.loading = false
                          return
                        }
                        saveDealIndicatorsSetting(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                      this.locationError(document)
                    }
                })
           },
          delIndicators(index) {
              this.form.indicatorsDetails.splice(index, 1)
          },
          addIndicators() {
            const row = {
              id: undefined,
              maintainId: undefined,
              indicatorName: undefined,
              indicatorDirection: undefined,
              targetValue: undefined,
              remark: undefined
            }
            if (!(this.form.indicatorsDetails instanceof Array)) {
              this.$set(this.form, 'indicatorsDetails', [])
            }
            this.$nextTick(() => {
              this.form.indicatorsDetails.push(row)
            })
          },
          changeHandle(val) {
            this.$nextTick(() => {
              getDealBase(val).then(res => {
                this.form.companyId = res.data.companyId
                this.form.companyName = res.data.company.companyName
              })
            })
          }
        }
    }
</script>
